﻿using System;
using System.Net;
using System.Linq;
using MetroDropBox.Shared;
using System.Diagnostics;

namespace MetroHoo.External.Yahoo
{
    public class YahooAuthToken : OAuthToken
    {
        public YahooAuthToken(string result)
        {
            originalString = result;

            var items = result.Split('&');
            if (items.Count() > 0)
            {
                foreach (var item in items)
                {
                    var kvp = item.Split('=');
                    if (kvp.Count() > 0)
                        switch (kvp[0].ToLowerInvariant())
                        {
                            case "oauth_token":
                                Token = WebUtility.UrlDecode( kvp[1]);
                                break;
                            case "oauth_token_secret":
                                Secret = WebUtility.UrlDecode( kvp[1]);
                                break;
                            case "oauth_expires_in":
                                ExpiresIn = WebUtility.UrlDecode( kvp[1]);
                                break;
                            case "oauth_session_handle":
                                SessionHandle = WebUtility.UrlDecode( kvp[1]);
                                break;
                            case "oauth_authorization_expires_in":
                                AuthExpiresIn = WebUtility.UrlDecode( kvp[1]);
                                break;
                            case "xoauth_yahoo_guid":
                                XAuthGUID = WebUtility.UrlDecode( kvp[1]);
                                break;
                            default:
                                break;
                        }
                }
                Debug.WriteLine("Token:");
                Debug.WriteLine(Token.Substring(0,80));
                Debug.WriteLine(Token.Substring(80));
                Debug.WriteLine("Secret: " + Secret);
                Debug.WriteLine("ExpiresIn: " + ExpiresIn);
                Debug.WriteLine("SessionHandle: " + SessionHandle);
                Debug.WriteLine("AuthExpiresIn: " + AuthExpiresIn);
                Debug.WriteLine("XAuthGUID: " + XAuthGUID);
            }
        }

        public string ExpiresIn { get; protected set; }

        public string SessionHandle { get; set; }

        public string AuthExpiresIn { get; set; }

        public string XAuthGUID { get; set; }
    }
}
